$(function () {
    //获取项目代码
    var fullUrl = location.href;
    var urlBranch = '';
    if(fullUrl.indexOf('localhost') >= 0 || fullUrl.indexOf('file:') >= 0) urlBranch = 'dev';
    else urlBranch = fullUrl.split('http://')[1].split('.')[0];
    new followupPlan().getProjectBaseInfo(urlBranch,function (res, status) {
        res = JSON.parse(res);
        if(res.code == '000'){
            localStorage.suid = JSON.parse(res.data).id;
            localStorage.projectName = JSON.parse(res.data).name;
            $('#projectName').text((JSON.parse(res.data).name || '诊后随访'));
            $('#logo').css('opacity',1);
        }else{
            $('#modal-error').css('opacity',1).html('网络错误');
        }
    });
    var urlParamObj = $$.util.urlParamToObj();
    if(urlParamObj.error == 1){
        $('#modal-error').css('opacity',1).html('登录信息过期，请重新登录');
    }
    setTimeout(function () {
        $('#password').val('');
        if(!localStorage.remember) $('#phone').val('');
    },100);
    $.material.init();
    createCode();
    if(localStorage.remember){
        $('#phone').val(localStorage.phone);
        $('.remember').find('input').attr('checked',true);
    }
});

$('#loGin').click(function(){/*用户或者密码为空时的提示*/
    var $modalerror= $('#modal-error');
     if($('#phone').val()==''||$('#password').val()==''){
         $modalerror.css('opacity',1).html('您的用户和密码不能为空');
     }
     else if($('#phone').val().match(/^1[3|4|5|7|8]\d{9}$/)==null){
         $modalerror.css('opacity',1).html('请输入正确的手机号码');
     }
     else if($('#password').val().length<6){
         $modalerror.css('opacity',1).html('请输入6-12位的密码');
        }
    else{
         clearWarning();
         new followupPlan().login($('#phone').val(),$('#password').val(),function(res,status){
             res = JSON.parse(res);
             console.log(res);
             if(status == 'success'){
                if(res.code == '000'){
                    //记住账号
                    if($('.remember').find('input').prop('checked')){
                        localStorage.remember = 'true';
                    }else{
                        localStorage.removeItem('remember');
                    }
                    localStorage.token = JSON.parse(res.data).token;
                    localStorage.sign = res.sign;
                    localStorage.userInfo = res.data;
                    //页面跳转
                    location.href='./html/plan/datalist.html';
                }else{
                    $modalerror.css('opacity',1).html(res.msg);
                }
             }else{
                 $modalerror.css('opacity',1).html('网络错误');
             }
         });
     }
});
/*清除弹出事件=====*/
function clearWarning(){
    $('#modal-error').html('alert');
    $('#modal-error').css({'opacity':0});
}
/*给回车键一个点击事件======*/
$('input').keyup(function(event){
    var e = event || window.event;
    if(e.keyCode==13){
        $('#loGin').click();
    }
});
/*手机输入失去焦点的时候，验证输入的是否为手机=====*/
$('#phone').blur(function(){
    var phonematch=$('#phone').val().match(/^1[3|4|5|7|8]\d{9}$/);
    var $modalerror=$('#modal-error');
    if($('#phone').val()!=''){
        if(phonematch==null){
            $modalerror.css('opacity',1).html('请输入正确的手机号码');
        } else {
            $modalerror.css('opacity',0).html('alert');
        }
    }else{
        $modalerror.css('opacity',0).html('alert');
    }
});
/*随机验证码==========================================*/
var code ; //在全局 定义验证码
function createCode(){/*创建一个新的验证码===*/
    code = [];
    var codeLength = 4;//验证码的长度
    var checkCode = document.getElementById("checkCode");
    checkCode.value = "";
    var selectChar = [2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'];
    for(var i=0;i<codeLength;i++) {
        var charIndex = Math.floor(Math.random()*32);
        code +=selectChar[charIndex];
    }
    if(code.length != codeLength){
        createCode();
    }
    checkCode.value = code;
}
function validate () {/*确认验证码是否正确的函数*/
    var inputCode = document.getElementById("input1").value.toUpperCase();
    if(inputCode.length <=0) {
        alert("请输入验证码！");
        return false;
    }
    else if(inputCode != code ){
        alert("验证码输入错误！");
        createCode();
        return false;
    }
    else {
        alert("成功！");
        return true;
    }
}
/*=================================================================================================*/